const path = require("path"); // 导入path模块
const resolve = (dir) => path.resolve(__dirname, dir);
// 输出html
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
    // 入口文件
    entry: "./src/index.tsx",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: "ts-loader",
                exclude: "/node_modules/",
            },
            {
                test: /\.scss?$/,
                use: [
                    "style-loader",
                    {
                        loader: "css-loader",
                        options: {
                            modules: {
                                localIdentName:
                                    "[name]__[local]___[hash:base64:5]",
                            },
                        },
                    },
                    "sass-loader",
                    "postcss-loader",
                ],
                exclude: [/node_modules/],
            },
        ],
    },
    resolve: {
        extensions: [".tsx", ".ts", ".js"],
        alias: {
            "@": resolve("src"),
        },
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, "./template/index.html"),
            filename: "index.html",
            title: "react",
        }),
    ],
    // 出口文件
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "bundle.js",
    },
    devServer: {
        static: {
            directory: path.resolve(__dirname, "dist"),
        },
        port: 8080,
        open: true,
        hot: true,
    },
    cache: {
        type: "filesystem",
        buildDependencies: {
            config: [__filename],
        },
    },
};
